<script type="text/javascript">
    function incval(){
        //alert('id');
        //alert(document.getElementById("1"));
        //alert(id);
        //v = document.getElementById(id).value;
        //alert(v);
    }
</script>
<?php

include(Kohana::find_file('views', 'resources'));

if ($blank) {
    if ($build->id != NULL) {
        echo '<label for="remaining">Remaining time: </label><p id="remaining"></p>';
        echo '<a href="' . URL::site('building') . '/cancelbuild/' . Request::current()->param('id') . '_' . $building->next_level . '">Cancel!</a>';
    } else {
        echo 'Avaible buildings:';
        foreach ($avaible as $a) {
            $b = ORM::factory('buildings', $a);
            echo '<br />' . ORM::factory('buildings', $a)->name . '<br />';
            $cost = ORM::factory('buildcost')->where('building_id', '=', $a)->find_all();
            foreach ($cost as $c) {
                echo ORM::factory('products', $c->product_id)->name . ' - ' . $c->quantity . '<br />';
            }
            echo 'Build time: ' . $b->build_time . '<br />';
            echo '<a href="' . URL::site('building') . '/build/' . Request::current()->param('id') . '_' . $a . '">Build!</a><br />';
        }
    }
} else {
    echo '<b>' . $building->name . ' - Level ' . $building->level . '</b><br />';
    echo $building->description . '<br />';

    if ($production != NULL) {
        echo '<b>Production:</b><br />';
        foreach ($production as $prod) {
            echo ucfirst(ORM::factory('products', $prod->product_id)->name) . ': ' . $prod->quantity . '/h<br />';
        }
    }

    if ($storage) {
        echo 'Raktáron:<br />';
        foreach ($storaged_goods as $goods) {
            echo $goods['prod_name'] . ': ' . round($goods['quantity']) . '<br />';
        }
    }

    if ($building->next_level != 0) {
        if ($build_cost != NULL) {
            echo '<b>Build cost:</b><br />';

            foreach ($build_cost as $bc) {
                echo ucfirst(ORM::factory('products', $bc->product_id)->name) . ': ' . $bc->quantity . '<br />';
            }

            echo 'Build time: ' . ORM::factory('buildings', $building->next_level)->build_time . '<br />';
            if ($build->id != NULL) {
                echo '<label for="remaining">Remaining time: </label><p id="remaining"></p>';
                echo '<a href="' . URL::site('building') . '/cancelbuild/' . Request::current()->param('id') . '_' . $building->next_level . '">Cancel!</a>';
            } else {
                echo '<a href="' . URL::site('building') . '/build/' . Request::current()->param('id') . '_' . $building->next_level . '">Upgrade!</a><br />';
            }
        }
    }
    
    if ($barrack) {
        echo '<form action="'.URL::site('unit/train').'" method="post" accept-charset="utf-8">';
        echo '<table>';
        foreach ($units as $unit) {
            echo '<tr>';
            echo '<td>';
            echo '<b>'.$unit->name . '</b><br />';
            echo 'Attack: '.$unit->attack . '<br />';
            echo 'Defense: '.$unit->defense . '<br />';
            echo 'Cost: '.$unit->cost . '<br />';
            echo 'Training time: '.$unit->training_time . '<br /><br />';
            echo '</td>';
            echo '<td>';
            echo '<center><a href="#" onClick="incval();">+</a>';
            echo '<img scr="'.URL::base().'media/images/minus.gif'.'" alt="-" onClick="decrease_value()"/></center>';
            echo '<input type="textfield" name="'.$unit->id.'" id="'.$unit->id.'" value=0>';
            echo '</td>';
            echo '</tr>';
        }
        echo '<tr>';
        echo '<td colspan=2>';
        echo '<center><input type="submit" value="Train!"></center>';
        echo '</td>';
        echo '</tr>';
        echo '</table>';
        echo '</form>';
    }
    
    echo '<a href="' . URL::site('building') . '/destroy/' . Request::current()->param('id').'">Destroy!</a><br />';
}
?>

<script type="text/javascript">
    var town_id = <?php echo Session::instance()->get('town_id') ?>;
    var place_id = <?php echo Session::instance()->get('place_id') ?>;
    var finish_time = <?php echo $build->finish_time ?>;
    var d;
    var current_time;
    var diff;
    
    function buildTime(){
        var text = document.getElementById('remaining');
        if(text!=null){
            d = new Date();
            current_time = Math.floor(d.getTime()/1000);
            diff = finish_time-current_time;
            current_time = Math.floor(d.getTime()/1000);
            text.innerHTML = secondsToTime(diff);
        }
        setTimeout("buildTime()", 1000);
        if(diff<=0){
            var oRequest = new XMLHttpRequest();
            var sURL  = "<?php echo URL::site('building') ?>/finishbuild/"+town_id+'_'+place_id;
            oRequest.open("GET",sURL,false);
            oRequest.setRequestHeader("User-Agent",navigator.userAgent);
            oRequest.send(null);
            window.location.reload();
        }
    }
    
    function secondsToTime(secs){
        var hours = Math.floor(secs / (60 * 60));
        var divisor_for_minutes = secs % (60 * 60);
        var minutes = Math.floor(divisor_for_minutes / 60);
        var divisor_for_seconds = divisor_for_minutes % 60;
        var seconds = Math.ceil(divisor_for_seconds);
        if(hours<10) hours='0'+hours;
        if(minutes<10) minutes='0'+minutes;
        if(seconds<10) seconds='0'+seconds;
        var time = hours+':'+minutes+':'+seconds;
        return time;
    }
    
    
    
    function decrease_value(){
        
    }
    
    function calculate_cost(){
        
    }
    
    window.onload = buildTime;
</script>